package jubaozhu.controller;

import jubaozhu.domain.Orders;
import jubaozhu.extra.base.BaseController;
import jubaozhu.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import zhibi.frame.mybatis.example.Example;
import zhibi.frame.mybatis.example.ExampleType;

import java.util.List;

@Controller
@RequestMapping("product")
public class ProductController extends BaseController {


    @Autowired
    private OrdersService ordersService;

    @RequestMapping("detail/{id}")
    public String detail(@PathVariable Integer id) {
        if (null == sessionUser()) return redirect("/view/login");
        return "product-" + id;
    }

    @PostMapping("apply")
    public String apply(Orders orders) {
        if (null == sessionUser()) return redirect("/view/login");
        ordersService.apply(orders, sessionUser());
        return redirect("/index");
    }

    @RequestMapping("order")
    public String order(String status, Model model) {
        if (null == sessionUser()) return redirect("/view/login");
        Example example = Example.getInstance()
                .addParam("status", status)
                .addParam("userid", sessionUser().getId())
                .addOrder("addtime", ExampleType.OrderType.DESC);
        List<Orders> ordersList = ordersService.selectByExample(example);
        model.addAttribute("ordersList", ordersList);
        return "order";
    }


}
